package com.zdxlz.fcmp.upms.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xingyuv.captcha.model.common.ResponseModel;
import com.xingyuv.captcha.model.vo.CaptchaVO;
import com.xingyuv.captcha.service.CaptchaService;
import com.zdxlz.fcmp.common.core.constant.enums.LoginTypeEnums;
import com.zdxlz.fcmp.common.core.constant.enums.ResCode;
import com.zdxlz.fcmp.common.core.exception.BizException;
import com.zdxlz.fcmp.common.core.util.R;
import com.zdxlz.fcmp.common.log.annotation.LoginLogRecord;
import com.zdxlz.fcmp.common.security.service.SysLoginService;
import com.zdxlz.fcmp.common.security.util.UserLoginInfoUtil;
import com.zdxlz.fcmp.upms.api.entity.SystemUser;
import com.zdxlz.fcmp.upms.api.service.ISystemMenuService;
import com.zdxlz.fcmp.upms.api.service.ISystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zdxlz.fcmp.common.core.constant.CommonConstants.*;

/**
 * @author aman
 * @description 系统登陆控制层
 * @date 2024/12/18 15:21
 */
@Slf4j
@Data
@RestController
@Tag(name = "登陆模块")
@LoginLogRecord(module = "登陆模块")
@RequestMapping("/api/v1/sys")
public class SystemLoginController {

    @Autowired
    private CaptchaService captchaService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private ISystemUserService systemUserService;

    @Autowired
    private ISystemMenuService systemMenuService;

    /**
     * @description 登陆系统
     * @author aman
     * @date 2025/1/17 09:54
     */
    @PostMapping("/login")
    @Operation(summary = "登陆系统")
    @LoginLogRecord(module = "登陆系统", value = "用户登陆系统，用户账号【{}】", db = true, field = {"mobile"}, loginType = LoginTypeEnums.LOGIN)
    public R login(@RequestBody SystemUser systemUser) {
        if (ObjectUtil.isEmpty(systemUser) || StrUtil.isBlank(systemUser.getMobile()) || StrUtil.isBlank(systemUser.getPassword())) {
            throw new BizException(ResCode.ERR_LOGIN_INFO_MISS.getCode(), ResCode.ERR_LOGIN_INFO_MISS.getReason());
        }
        log.info("登陆账号：{}", systemUser.getMobile());
        return R.ok(sysLoginService.login(systemUser.getMobile(), systemUser.getPassword(), null, null), LOGIN_SUCCESS);
    }

    /**
     * @description 退出系统
     * @author aman
     * @date 2025/1/17 09:54
     */
    @PostMapping("/logout")
    @Operation(summary = "退出系统")
    @LoginLogRecord(module = "退出系统", value = "用户退出系统，用户账号【{}】", db = true, field = {"mobile"}, loginType = LoginTypeEnums.LOGOUT)
    public R logout(@RequestHeader("Authorization") String authorization) {
        return R.ok(sysLoginService.logout(authorization), OPERATION_SUCCESS);
    }

    /**
     * @description 退出系统
     * @author aman
     * @date 2025/1/17 09:54
     */
    @PostMapping("/token/refresh")
    @Operation(summary = "刷新ACCESS_TOKEN")
    public R refreshAccessToken(@RequestHeader("Authorization") String authorization, @RequestParam String refreshToken) {
        return R.ok(sysLoginService.refreshAccessToken(authorization, refreshToken), OPERATION_SUCCESS);
    }

    /**
     * @description 登陆时是否显示验证码
     * @author aman
     * @date 2025/1/17 09:54
     */
    @PostMapping(value = "/captcha/enable")
    @Operation(summary = "是否显示验证码")
    public R enableCaptcha(@RequestParam String mobile) {
        return R.ok(systemUserService.enableCaptcha(mobile), OPERATION_SUCCESS);
    }

    /**
     * @description 获取图形验证码
     * @author aman
     * @date 2025/1/17 09:52
     */
    @PostMapping({"/captcha"})
    @Operation(summary = "获取图形验证码")
    public R getCaptcha(@RequestBody CaptchaVO data) {
        ResponseModel responseModel = captchaService.get(data);
        return R.ok(responseModel.getRepData(), OPERATION_SUCCESS);
    }

    /**
     * @description 校验图形验证码
     * @author aman
     * @date 2025/1/17 09:51
     */
    @PostMapping("/captcha/check")
    @Operation(summary = "校验图形验证码")
    public R check(@RequestBody CaptchaVO data) {
        ResponseModel check = captchaService.check(data);
        return R.ok(check.getRepData(), OPERATION_SUCCESS);
    }

    /**
     * @Description：用户登陆成功后，获取系统菜单信息
     * @Author：aman
     * @Date：2025/1/17 09:50
     * @ParamsType：[]
     * @Return：com.example.springsecurity.common.response.ResponseResult<java.util.List<com.example.springsecurity.vo.login.SystemMenuTreeVo>>
     */
    @GetMapping(value = "/getMenuList")
    @Operation(summary = "获取系统菜单信息")
    @LoginLogRecord(module = "获取系统菜单信息", value = "用户登陆成功获取系统菜单信息", loginType = LoginTypeEnums.LOGIN)
    public R getMenuList() {
        List<String> userLoginPermissions = UserLoginInfoUtil.getUserLoginPermissions();
        return R.ok(systemMenuService.getMenuList(userLoginPermissions), OPERATION_SUCCESS);
    }
}
